/**
 * Created by no1 on 2015/9/5.
 */

'use strict';
let DD = require('./daoLoader');
let F = require('../function')
let Base = require('./Base');
let util = require("util");
let orderTicketDao = new DD.mysqlDB.OrderTicketDao();
let OrderDispatcher = require('./order');

function OrderTicketDispatcher(data) {
  data = data || {};
  data.dao = data.dao || orderTicketDao;
  data.esDao = data.esDao || {};
  data.redisDao = data.redisDao || {};
  data.modelName = data.modelName || 'orderTicket';
  data.modelType = data.modelType || 'mysql';
  this.data = data;
  Base.call(this, this.data);
};
util.inherits(OrderTicketDispatcher, Base);

/**
 *@todo 添加瓷肌券
 *@param  {Object}  fields  所有参数
 */
OrderTicketDispatcher.prototype.add = function*(fields) {
  let parameters = {};

  parameters.order_id = fields.order_id;
  //调用orderDispatcher
  let retOrder = yield F.GetDataFromDispatcher('order','findOneForDis',{orderid:fields.order_id});
  if(retOrder.error ==1){
    return F.returnData(1, retOrder.msg, retOrder.data);
  }
  parameters.vid = retOrder.data.vendorid || 0;
  parameters.money = fields.money;

  //判断时间是否符合 //let date = parseInt(new Date(new Date().format('yyyy-MM-dd hh:mm:ss') + ' CST+800').getTime() / 1000);
  //if (fields.valid_start_date.length != 10 || fields.valid_end_date.length != 10) return F.returnData(1, '时间格式有误');
  parameters.valid_start_date = fields.valid_start_date;
  parameters.valid_end_date = fields.valid_end_date;
  parameters.ticket_code = fields.ticket_code;//算法得到,唯一
  parameters.status = 0;//0:未消费；1：已消费；2：已取消 3：退款中 4: 已退款
  parameters.appraise_state = 0;//0 未使用  1 可评价 1 已评价

  //保存数据
  let ret = yield this.dao.findAndSave({ticket_code: parameters.ticket_code}, parameters);
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.data);
  return F.returnData(0, ret.msg, ret.data);
};

/**
 * @todo 更新瓷肌券
 * { "id":xxx,
 *   "update":{foo:bar}
 * }
 */
OrderTicketDispatcher.prototype.update = function*(fields) {
  var where = {id: fields.id};
  if(!!fields.update.vid){
    delete fields.update.vid;
  }
  var update = fields.update;
  let ret = yield this.dao.update(where, update);
  return F.returnData(0, 'ok', ret);
};

/**
 * @todo 查询瓷肌券
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
OrderTicketDispatcher.prototype.getTicket = function*(fields) {
  let where = {};
  if(!!fields.id){
    let id = fields.id;
    if (Array.isArray(id)){
      where.order_id = {$in: id};
    }else{
      where.order_id = id;
    }
  }
  //通过status查询
  if(!!fields.status){
    where.status = fields.status;
  }

  //通过商家id查询
  if(!!fields.vid){
    where.vid = fields.vid;
  }

  //通过瓷肌券码查询
  if(!!fields.ticket_code){
    where.ticket_code = fields.ticket_code;
  }

  let page = fields.page || 1;
  let pageSize = fields.pageSize || 10;
  let ret = yield this.dao.getByQuery(where,page,pageSize);
  return F.returnData(0, 'ok', ret);
};

/**
 * @todo 查询瓷肌券详情
 * @param fields  * {ticket_code:1}
 * @returns {{error, msg, data}|{error, msg}}
 */
OrderTicketDispatcher.prototype.ticketDetail = function*(fields) {
  let where = {ticket_code: fields.ticket_code};
  let ret = yield this.dao.getByQuery(where);
  return F.returnData(0, 'ok', ret);
};

module.exports = OrderTicketDispatcher;